package com.sjk.service.impl;

/*
  @author 86136
 * @create 2023/5/15 21:12
 */


import com.sjk.mapper.UserDao;
import com.sjk.pojo.User;
import com.sjk.service.UserService;
import com.sjk.utils.EncryptAlgorithm;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    // 密文
    @Override
    public List<User> getAll0(int page,int pageSize) {
        return userDao.selectAll0(page,pageSize);
    }

    // 明文
    @Override
    public List<User> getAll1(int page,int pageSize) {
        return userDao.selectAll1(page,pageSize);
    }

    // 手机号查找
    @Override
    public List<User> getByPhonenum(String phonenum,int authority) {
        // 转换
        String phonenum_encrypt = EncryptAlgorithm.encrypt(phonenum);

        if(authority == 1) {
            return userDao.selectByAllPhonenum0(phonenum_encrypt);
        } else if(authority == 2){
            return userDao.selectByAllPhonenum1(phonenum_encrypt);
        } else {
            return null;
        }
    }

    // 手机尾4号查找
    @Override
    public List<User> getByPhonenum4(String phonenum4,int authority) {
        // 转换
        String phonenum4_encrypt = EncryptAlgorithm.encrypt(phonenum4);

        if(authority == 1) {
            return userDao.selectBy4Phonenum0(phonenum4_encrypt);
        } else if(authority == 2){
            return userDao.selectBy4Phonenum1(phonenum4_encrypt);
        } else {
            return null;
        }

    }

    // 身份证号查找
    @Override
    public List<User> getById(String id,int authority) {
        // 转换
        String id_encrypt = EncryptAlgorithm.encrypt(id);;

        if(authority == 1) {
            return userDao.selectByAllId0(id_encrypt);
        } else if(authority == 2){
            return userDao.selectByAllId1(id_encrypt);
        } else {
            return null;
        }
    }

    // 身份证尾号6位查找
    @Override
    public List<User> getById6(String id6, int authority) {
        // 转换
        String id6_encrypt = EncryptAlgorithm.encrypt(id6);;

        if(authority == 1) {
            return userDao.selectBy6Id0(id6_encrypt);
        } else if(authority == 2) {
            return userDao.selectBy6Id1(id6_encrypt);
        } else {
            return null;
        }
    }

    // 删除前手机号查找
    @Override
    public List<User> deleteGetByPhonenum(String phonenum){
        // 转换
        String phonenum_encrypt = EncryptAlgorithm.encrypt(phonenum);

        return userDao.deleteSelectByAllPhonenum(phonenum_encrypt);
    }

    // 删除用户
    @Override
    public void deleteByPhonenum(String phonenum){
        // 转换
        String phonenum_encrypt = EncryptAlgorithm.encrypt(phonenum);;
        userDao.deleteByAllPhonenum(phonenum_encrypt);
    }

    // 更新前手机号查找
    @Override
    public List<User> updateGetByPhonenum(String phonenum){
        // 转换
        String phonenum_encrypt = EncryptAlgorithm.encrypt(phonenum);

        return userDao.updateSelectByAllPhonenum(phonenum_encrypt);
    }

    // 更新用户信息
    @Override
    public void updateUser(User user){
        // 对手机号重新加密并且更新
        String phonenum = user.getPhonenum();
        String phonenum4 = phonenum.substring(phonenum.length()-4);
        String phonenum_encrypt = EncryptAlgorithm.encrypt(phonenum);
        String phonenum4_encrypt = EncryptAlgorithm.encrypt(phonenum4);
        user.setPhonenum_encrypt(phonenum_encrypt);
        user.setPhonenum4_encrypt(phonenum4_encrypt);

        userDao.updateUser(user);
    }

    // 添加用户
    @Override
    public void addUser(User user){
        // 对用户信息的处理
        String phonenum = user.getPhonenum();
        String phonenum4 = phonenum.substring(phonenum.length()-4);
        String id = user.getId();
        String id6 = id.substring(id.length()-6);

        // 初始化密码
        user.setPassword(EncryptAlgorithm.encrypt("abc"+id6));

        // 加密手机号和手机尾号
        String phonenum_encrypt = EncryptAlgorithm.encrypt(phonenum);
        String phonenum4_encrypt = EncryptAlgorithm.encrypt(phonenum4);
        user.setPhonenum_encrypt(phonenum_encrypt);
        user.setPhonenum4_encrypt(phonenum4_encrypt);

        // 加密身份证号和尾号
        String id_encrypt = EncryptAlgorithm.encrypt(id);
        String id6_encrypt = EncryptAlgorithm.encrypt(id6);
        user.setId_encrypt(id_encrypt);
        user.setId6_encrypt(id6_encrypt);

        user.setAuthority(0);

        userDao.insertUser(user);
    }
}
